package com.tiger.utils;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

import org.apache.commons.lang3.time.FastDateFormat;

public class DateUtils {
	//public static final FastDateFormat DATETIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
	  
	//public static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");

	public static boolean isSameDay(Date date1, Date date2) {
		return org.apache.commons.lang3.time.DateUtils.isSameDay(date1, date2);
	}
	
	public static LocalDate toLocalDate(Date date) {
		return date == null ? null : date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
	}
	public static LocalTime toLocalTime(Date date) {
		return date == null ? null : date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
	}
	public static Date toDate(LocalDateTime date) {
		return date == null ? null : Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
	}
	public static enum Format {
		DATEI("yyyyMMdd"),
		DATE("yyyy-MM-dd"),
		DATETIME("yyyy-MM-dd HH:mm:ss"),
		DATEHOURMIN("yyyy-MM-dd HH:mm");

		private final FastDateFormat format;

		Format(String formatText) {
			format = FastDateFormat.getInstance(formatText);
		}
		public Date parse(String s) {
			try {
				return format.parse(s);
			} catch (ParseException e) {
				return null;
			}
		}
		public String format(Date date) {
			return format.format(date);
		}

		public String now() {
			return format(new Date());
		}
	};
}
